﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BindCodes
{
    public partial class FrmAllRltCodes : Form
    {
        string code24, codeRlt;
        public Dictionary<string, string> CodesBinded { get; set; }//key=关联码，value=24位码

        public FrmAllRltCodes()
        {
            InitializeComponent();
        }

        private void txtScan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string c = txtScan.Text.Trim();
                txtScan.Text = "";

                AppendCode(c);
            }
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(code24) || string.IsNullOrEmpty(codeRlt))
            {
                txtTip.AppendText("请先扫描或输入瓶盖码、追溯码！\r\n");
                return;
            }

            if (CodesBinded == null)
            {
                txtTip.AppendText("无关联数据！\r\n");
                return;
            }

            if (CodesBinded[codeRlt].Equals(code24))
            {
                txtTip.Text = "";
                code24 = "";
                codeRlt = "";
                MessageBox.Show("关联成功！");
            }
            else
            {
                txtTip.AppendText("关联失败！\r\n");
  
            }

        }

        private void FrmAllRltCodes_Load(object sender, EventArgs e)
        {
            if (CodesBinded!=null)
            {
                foreach (var item in CodesBinded)
                {
                    lbCodes.Items.Add($"{item.Key} -> {item.Value}");
                }
            }
        }

        public void AppendCode(string c)
        {
            if (CodesBinded != null)
            {
                if (CodesBinded.ContainsKey(c))
                {
                    codeRlt = c;
                    txtTip.AppendText($"瓶盖码：{c}\r\n");
                }
                else if (CodesBinded.Values.Contains(c))
                {
                    code24 = c;
                    txtTip.AppendText($"追溯码：{c}\r\n");
                }
                else
                {
                    txtTip.AppendText("查无此数据！\r\n");
                }
            }
            else
            {
                txtTip.AppendText("无关联数据！\r\n");
            }
        }
    }
}
